//
//  AXFEditCell.swift
//  SpuerYangAngWang
//
//  Created by 王嘉涛 on 2016/12/27.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class AXFEditCell: UITableViewCell,UITextFieldDelegate {

    // 名字
    @IBOutlet weak var nameTextFiled: UITextField!
    // 电话号
    @IBOutlet weak var phoneNumTextFiled: UITextField!
    // 城市
    @IBOutlet weak var cityTextFiled: UITextField!
    // 地址
    @IBOutlet weak var ressTextFiled: UITextField!
    // 详细地址
    @IBOutlet weak var detailedTextFiled: UITextField!
    // 男士按钮
    @IBOutlet weak var manBtn: UIButton!
    // 女士按钮
    @IBOutlet weak var womenBtn: UIButton!
    
    
    var cellClosure:((AXFAddressModel?)->())?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        nameTextFiled.delegate = self
        phoneNumTextFiled.delegate = self
        cityTextFiled.delegate = self
        ressTextFiled.delegate = self
        detailedTextFiled.delegate = self
        
        // 选中男士女士
//        self.manBtn.isSelected = false;
        manBtn.addTarget(self, action: #selector(clickManBtn), for: .touchUpInside)
        womenBtn.addTarget(self, action: #selector(clickWomenBtn), for: .touchUpInside)
    }
    
    @objc private func clickManBtn() {
        manBtn.isSelected = true
        womenBtn.isSelected = false
    }
    @objc private func clickWomenBtn() {
        manBtn.isSelected = false
        womenBtn.isSelected = true
    }

    var model:AXFAddressModel = AXFAddressModel()
    
    var addressModel : AXFAddressModel? {
        
        didSet{
            nameTextFiled.text = addressModel?.accept_name
            phoneNumTextFiled.text = addressModel?.telphone
            cityTextFiled.text = addressModel?.city_name
            let str = addressModel?.address
            let ress = str?.components(separatedBy: " ").first
            let detailed = str?.components(separatedBy: " ").last
            ressTextFiled.text = ress
            detailedTextFiled.text = detailed
            
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
     
        model.accept_name = nameTextFiled.text
        model.telphone = phoneNumTextFiled.text
        model.city_name = cityTextFiled.text
        model.address = ressTextFiled.text
        model.district_name = detailedTextFiled.text
        cellClosure?(model)
        
    }
    
}
